//
//  RCMapViewController.m
//  MapTest
//
//  Created by Miles on 10-05-13.
//  Copyright 2010 Miles Thorogood
//  greenmeat.net. All rights reserved.
//

#import "RCMapViewController.h"
#import "RCMapView.h"
#import "RCCommunicationController.h"
#import "RCMapAnnotation.h"

@implementation RCMapViewController

@synthesize aMap ;
@synthesize dataDictionary ;
@synthesize arrayOfAds ;
@synthesize comController ;

- (id) init {
	self = [super init] ;
	if (self) {
		CGRect frame = CGRectMake(0, 0, 320, 480) ; 
		RCMapView *map = [[RCMapView alloc] initWithFrame:frame] ;
		self.aMap = map ;
		[self.view addSubview:aMap] ;
		[map release] ;
		
		arrayOfAds = [[NSMutableArray alloc] init] ;
		// start the communication and location
		self.comController = [[RCCommunicationController alloc] initWithDelegate:self] ;

	}
	return self ;
}
- (void) dealloc {
	[aMap release] ;
	[dataDictionary release] ;
	[arrayOfAds release];
	[comController release] ;
	[super dealloc] ;
}

// from RCCommunicationController
- (void) locationDataReceived:(id) handler {
	self.dataDictionary = [handler mediaDictionary];
	[handler release];
	
	printf("received data from location");
	NSLog(@"%@", [dataDictionary objectForKey:@"image"]) ;
	
	MKCoordinateSpan span;
    span.latitudeDelta=.005;
    span.longitudeDelta=.005;
    
	CLLocation *newLocation = (CLLocation *) [dataDictionary objectForKey:@"location"] ;
    MKCoordinateRegion region;
    region.center = newLocation.coordinate;
    region.span=span;
    
    [aMap.aMapView setRegion:region animated:TRUE];
	
	NSString *type = (NSString *) [dataDictionary objectForKey:@"type"] ; // check if good
	if (![type isEqualToString:@""]) {
		printf("Got an add");
		RCMapAnnotation *mAnn = [[RCMapAnnotation alloc] initWithDictionary:dataDictionary] ;
		[arrayOfAds addObject:mAnn] ;
		//mAnn.title = @"adsij" ;
		//mAnn.subtitle = @"sdsds" ;
		[aMap.aMapView addAnnotation:mAnn] ;
	}
	
}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
	NSLog(@"I've been tapped. Show some details.");
	if (aMap.aMapView.userLocation == annotation){
		return nil;
	}
	
	NSString *identifier = @"MY_IDENTIFIER";
	
	MKAnnotationView *annotationView = [aMap.aMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
	if (annotationView == nil){
		annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]
						  autorelease];
		annotationView.image = [UIImage imageNamed:@"?.png"];
		annotationView.canShowCallout = YES;
		annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
		annotationView.leftCalloutAccessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star.png"]] autorelease];
		
	}
	return annotationView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
	NSLog(@"I've been tapped. Show some details.");
	
	RCMapAnnotation* ann=(RCMapAnnotation*)view.annotation;
	
	//self.currentPinID=ann.pinID;
	//[contentViewController setMyCurrentPinID:42];
	//[self presentModalViewController:contentViewController animated:YES];
	
	
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
 
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


@end
